<template>
    <view class="page-body">
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="上架房源" name="first">
              <house-table :options="options" :list="tableList" @change="getHouseList"></house-table>
            </el-tab-pane>
            <el-tab-pane label="审核房源" name="second">审核房源</el-tab-pane>
            <el-tab-pane label="下架房源" name="third">下架房源</el-tab-pane>
            <el-tab-pane label="房源配置" name="fourth">
                <el-form :inline="true">
                    <el-form-item>
                        <el-input v-model="config"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @tap="addConfig('house_dic')">添加房源配置</el-button>
                    </el-form-item>
                </el-form>
                <el-tag
                style="margin-left: 20px;margin-top: 20px"
                  v-for="tag in dicList"
                  :key="tag.name"
                  @close="deleteTag(tag)"
                  closable>
                  {{tag.name}}
                </el-tag>
            </el-tab-pane>
            <el-tab-pane label="房源地铁线" name="fivth">
              <el-form :inline="true">
                  <el-form-item>
                      <el-input v-model="config"></el-input>
                  </el-form-item>
                  <el-form-item>
                      <el-button @tap="addConfig('ditie_dic')">添加地铁配置</el-button>
                  </el-form-item>
              </el-form>
              <el-tag
              style="margin-left: 20px;margin-top: 20px"
                v-for="tag in dicList"
                :key="tag.name"
                @close="deleteTag(tag)"
                closable>
                {{tag.name}}
              </el-tag>
            </el-tab-pane>
            <el-tab-pane label="房源小区" name="sixth">
              <el-form :inline="true">
                  <el-form-item>
                      <el-input v-model="config" placeholder="请输入小区名称"></el-input>
                  </el-form-item>
                  <el-form-item>
                      <el-input v-model="letter" placeholder="请输入首字母"></el-input>
                  </el-form-item>
                  <el-form-item>
                      <el-button @tap="addConfig('xiaoqu_dic')">添加小区配置</el-button>
                  </el-form-item>
              </el-form>
              <el-tag
              style="margin-left: 20px;margin-top: 20px"
                v-for="tag in dicList"
                :key="tag.name"
                @close="deleteTag(tag)"
                closable>
                {{tag.name}}
              </el-tag>
            </el-tab-pane>
        </el-tabs>
    </view>
</template>
<script>
var that;													// 当前页面对象
var vk = uni.vk;									// vk实例
import houseTable from './house-table.vue'
export default {
  name: 'house',
  components: {
    houseTable
  },
    data() {
        // 页面数据变量
        return {
            activeName: 'first',
            config: '',
            letter: '',
            dicList: [],
            options: [],
            tableList: [],
            pageNum: 1
        }
    },
    // 监听 - 页面每次【加载时】执行(如：前进)
    onLoad(options = {}) {
        that = this;
        vk = that.vk;
        this.getXiaoqu()
      this.getHouseList()
    },
    // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
    onReady() {

    },
    // 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
    onShow() {
     
    },
    // 监听 - 页面每次【隐藏时】执行(如：返回)
    onHide() {


    },
    // 函数
    methods: {
      async getXiaoqu() {
        let info2 = await this.vk.callFunction({
          url: 'user/kh/dicGetList',
          title: '请求中...',
          isRequest: true,
          data: {
            type: 'xiaoqu_dic'
          }
        });
        if (info2.code == 0) {
          if (info2.rows.length > 0) {
            this.options = info2.rows
          }
        }
      },
      async getHouseList() {
        let info2 = await this.vk.callFunction({
          url: 'user/kh/houseList',
          title: '请求中...',
          isRequest: true,
          data: {
            pageNum: this.pageNum
          }
        });
        if (info2.code == 0) {
          if (info2.rows.length > 0) {
            this.tableList = info2.rows
          }
        }
      },
        handleClick(tab) {
        this.activeName = tab.name
        console.log(tab.name, 'tab.name')
        this.config = ''
        if (this.activeName == 'first') {
          this.pageNum = 1
          this.getHouseList()
        }
        if (this.activeName == 'fourth') {
          this.getDicList('house_dic')
        }
        if (this.activeName == 'fivth') {
          this.getDicList('ditie_dic')
        }
        if (this.activeName == 'sixth') {
          this.getDicList('xiaoqu_dic')
        }
      },
      deleteTag(item) {
        this.vk.confirm("确定要删除标签吗", (res) => {
          if (res.confirm) {
            // 点击确定按钮后的回调
            this.deleteTagFn(item)
          }
        });
      },
      async deleteTagFn(item) {
        let info = await this.vk.callFunction({
            url: 'user/kh/dicDelete',
            title: '请求中...',
            isRequest: true,
            data: {
              _id: item._id
            }
        })
        if (info.code == 0) {
            this.getDicList(item.type)
        }
      },
      async getDicList(type) {
        let info = await this.vk.callFunction({
            url: 'user/kh/dicGetList',
            title: '请求中...',
            isRequest: true,
            data: {
              type
            }
        })
        if (info.code == 0) {
            this.dicList = info.rows
        }
      } ,
      async addConfig(type) {
        
        // let arr6 = []
        // for (let i = 0; i < 19; i++) {
        //   if (i == 18) {
        //     arr6.push({
        //       config: '浦江',
        //       type: 'ditie_dic'
        //     });
        //   } else {
        //     arr6.push({
        //       config: (i + 1),
        //       type: 'ditie_dic'
        //     });
        //   }
        // }
        // for (let i = 0; i < arr6.length; i++) {
        //   await this.vk.callFunction({
        //       url: 'user/kh/dicAdd',
        //       title: '请求中...',
        //       isRequest: true,
        //       data: {
        //           name: arr6[i].config,
        //           type: arr6[i].type
        //       }
        //   })
        // }
        if (!this.config) {
            return uni.showToast({
                title: '请填写配置',
                icon: 'none'
            })
        }
        let obj = {
          name: this.config,
          type
        }
        if(this.letter) {
          obj.letter = this.letter
        }
        let info = await this.vk.callFunction({
            url: 'user/kh/dicAdd',
            title: '请求中...',
            isRequest: true,
            data: obj
        })
        if (info.code == 0) {
            uni.showToast({
                title: '添加成功',
                icon: 'none'
            })
            this.config = ''
            this.getDicList(type)
        }
      }
    },
    // 过滤器
    filters: {

    },
    // 计算属性
    computed: {

    }
}
</script>
<style lang="scss" scoped>

</style>